﻿
#region Usings

using PolyGameEngine.GamePieces.Common;
using PolyGameEngine.Tools;

using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;
using FarseerPhysics.Factories;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

#endregion

namespace PolyGameEngine.GamePieces
{
    public class CircleUnit : Unit
    {
        public CircleUnit(PhysicsGame PhysicsGame, World World, Material Material, Vector2 Position, float Rotation, float Radius, float DrawingDepth, int saveLineIndex, string GPID)
            : base(PhysicsGame, World, Material, Position, Rotation, DrawingDepth, saveLineIndex, GPID)
        {
            FixtureFactory.AttachCircle(ConvertToSim(Radius), 1, body);
        }
        

        public override void HandleInput()
        {

            int upOrDown = isNormalJumpUp ? -1 : 1;

            if (!canJump)
            {
                if (InputMan.IsKeyDown(Keys.A))
                    body.ApplyForce(new Vector2(-0.7f, 0));
                if (InputMan.IsKeyDown(Keys.D))
                    body.ApplyForce(new Vector2(0.7f, 0));
            }
            if (InputMan.IsKeyDown(Keys.A))
            {
                body.ApplyTorque(upOrDown * .2f);
            }
            if (InputMan.IsKeyDown(Keys.D))
                body.ApplyTorque(upOrDown * -.2f);
          
            if (InputMan.OnPress(Keys.Space))
            {
                if (canJump)
                {
                    AudioMan.PlayCue("Jump");
                    body.ApplyLinearImpulse(new Vector2(0, 2.2f * upOrDown));
                    return;
                }


            }
        }

        public override void Update(GameTime gameTime)
        {
            if (canJump)
                AudioMan.RollUpdate(body.AngularVelocity);
            else
                AudioMan.RollUpdate(0);

            base.Update(gameTime);
        }


        public void PlaySound(string sound)
        {
            if (body.LinearVelocity.Y >= 0.3)
                AudioMan.PlayCue(sound);
        }


        public override bool HandleDefaultCollision(Fixture ThisFixture, Fixture OtherFixture, Contact Contact)
        {
            GamePiece otherGP;

            if (OtherFixture.UserData != null && OtherFixture.UserData.GetType().IsSubclassOf(typeof(GamePiece)))
            {
                otherGP = (GamePiece)OtherFixture.UserData;


                if ((!OtherFixture.IsSensor) && (otherGP.GPID != "") && (otherGP.GPID != "Water") && (otherGP.GPID != "Lava") && (otherGP.GPID != "Oil") && (otherGP.GPID != "QuickSand") && (otherGP.GPID != "Teleport") && (otherGP.GPID != "GravityShift") && (otherGP.GPID != "Finnish"))
                {

                    if (material.Identifier == "Wood")
                        PlaySound("WoodImpact");
                    else if (material.Identifier == "Steel")
                        PlaySound("SteelImpact");
                    else if (material.Identifier == "Rubber")
                        PlaySound("RubberImpact");
                    else if (material.Identifier == "Plastic")
                    {
                        if (otherGP.GPID != "Spike")
                            PlaySound("PlasticImpact");
                    }

                }
                else if (otherGP.GPID == "GravityShift")
                    AudioMan.PlayCue("Teleport");
                else if (otherGP.GPID == "QuickSand")
                    AudioMan.PlayCue("SandImpact");
                else if ((otherGP.GPID == "Water") || (otherGP.GPID == "Oil"))
                    AudioMan.PlayCue("WaterLeave");

            }

            return base.HandleDefaultCollision(ThisFixture, OtherFixture, Contact);
        }

    }
}
